<?php
namespace App\Repositories\System;
use App\Common\Utils\BaseRepository;
use App\Models\ActivityInfo;
use App\Models\Participant;
use Illuminate\Support\Facades\Auth;

/**
 * 参加人员信息
 */
class ParticipantRepository extends BaseRepository
{
    protected $model;

    public function __construct(Participant $participant)
    {
        $this->model = $participant;
    }

    /**
     * @Description: 添加数据
     * @Author: kzj
     * @datetime: 2023-02-28
     */
    public function addParticipant($activityInfoId, $ids, $type = 'PARTICIPANT')
    {
        $items = [];
        $user = Auth::user();
        $now = now()->toDateTimeString();
        
        $activity = ActivityInfo::whereId($activityInfoId)->first();
        
        Participant::where('activity_info_id', $activityInfoId)->where('participant_type', $type)->delete();

        foreach($ids as $k => $v){
            $items[$k]['activity_info_id'] = $activityInfoId;
            $items[$k]['activity_category'] = $activity->activity_category;
            $items[$k]['user_id'] = $v;
            $items[$k]['participant_type'] = $type;
            $items[$k]['name'] = $activity->name;
            $items[$k]['created_at'] = $now;
            $items[$k]['updated_at'] = $now;
            $items[$k]['creator_id'] = $user->id;
            $items[$k]['updater_id'] = $user->id;
        }

        Participant::insert($items);
    }

}